﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VQWEB.administrator.sources;
using VQData;
using System.Data;

namespace VQWEB.administrator.english.lectures
{
    public partial class lecture_level_add : System.Web.UI.Page
    {
        EnglishData EnglishData = new EnglishData();
        eng_lectures_level eng_lectures_level = null;
        int Id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (RouteData.Values["Id"] != null)
                int.TryParse(RouteData.Values["Id"].ToString(), out Id);
            if (Id > 0)
            {
                frmLectureLevel.DefaultMode = FormViewMode.Edit;
                eng_lectures_level = EnglishData.GetLectureLevelById(Id);
                //news = NewsData.GetNewsById(Id);
                frmLectureLevel.DataSource = new List<eng_lectures_level> { eng_lectures_level };
                frmLectureLevel.DataBind();
            }
            else frmLectureLevel.DefaultMode = FormViewMode.Insert;
        }

        protected void frmLectureLevel_ItemCreated(object sender, EventArgs e)
        {
            var control = frmLectureLevel.Row.FindControl("ltrCourse") as Literal;
            control.Text = ShowCourse();
            
        }

        private string ShowCourse() {
            int Id = 0;
            if (eng_lectures_level != null && eng_lectures_level.course_id.HasValue)
            {
                Id = eng_lectures_level.course_id.Value;  
            }
            string html = string.Empty;
            DataTable table = EnglishData.ViewCourse();
            if (table != null) {
                html += "<select name=\"course_id\" class=\"form-control\">";
                foreach (DataRow item in table.Rows)
                {
                    string selected = string.Empty;
                    int course_id = (int)item["course_id"];
                    string course_title = (string)item["course_title"];                   
                    if (Id == course_id)
                        selected = "selected=\"selected\"";
                    html += "<option " + selected + " value=" + course_id + ">" + course_title + "</option>";
                }
                html += "</select>";
            }
            return html;
        }
    }
}